import { createRouter, createWebHistory } from 'vue-router'
import { MessagePlugin } from 'tdesign-vue-next';

const routes = [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login/Login.vue'),
    },
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Home/Home.vue'),
      redirect: '/dashboard/workbench',
      children:[
          {
            path: '/dashboard/workbench',
            name: 'dashboard',
            component: () => import('@/views/Dashboard/Workbench.vue'),
          },
          {
            path: '/dashboard/analysis',
            name: 'analysis',
            component: () => import('@/views/Dashboard/Analysis.vue'),
          },
          {
            path: '/featureCenter/imageCrop',
            name: 'imageCrop',
            component: () => import('@/views/FeatureCenter/ImageCrop.vue'),
          },
          {
            path: '/featureCenter/excelFile',
            name: 'excelFile',
            component: () => import('@/views/FeatureCenter/ExcelFile.vue'),
          },
          {
            path: '/featureCenter/qrCode',
            name: 'qrCode',
            component: () => import('@/views/FeatureCenter/QRCode.vue'),
          },
          {
            path: '/featureCenter/richTextCompiler',
            name: 'richTextCompiler',
            component: () => import('@/views/FeatureCenter/RichTextCompiler.vue'),
          },
          {
            path: '/featureCenter/puzzle',
            name: 'puzzle',
            component: () => import('@/views/FeatureCenter/Puzzle.vue'),
          },
          {
            path: '/featureCenter/prettyInput',
            name: 'prettyInput',
            component: () => import('@/views/FeatureCenter/PrettyInput.vue'),
          },
          {
            path: '/featureCenter/loadingDemo',
            name: 'loadingDemo',
            component: () => import('@/views/FeatureCenter/LoadingDemo.vue'),
          },
          {
            path: '/featureCenter/switchDemo',
            name: 'switchDemo',
            component: () => import('@/views/FeatureCenter/SwitchDemo.vue'),
          },
          {
            path: '/personalCenter/personalCenter',
            name: 'personalCenter',
            component: () => import('@/views/PersonalCenter/PersonalCenter.vue'),
          },
          {
              path: '/result/404',
              name: 'result404',
              component: () => import('@/views/result/404/index.vue'),
          },
          {
              path: '/result/403',
              name: 'result403',
              component: () => import('@/views/result/403/index.vue'),
          },
          {
              path: '/result/500',
              name: 'result500',
              component: () => import('@/views/result/500/index.vue'),
          },
          {
              path: '/result/fail',
              name: 'resultFail',
              component: () => import('@/views/result/fail/index.vue'),
          },
          {
              path: '/result/success',
              name: 'resultSuccess',
              component: () => import('@/views/result/success/index.vue'),
          },
          {
              path: '/result/browser-incompatible',
              name: 'resultBrowserIncompatible',
              component: () => import('@/views/result/browser-incompatible/index.vue'),
          },
          {
              path: '/result/network-error',
              name: 'resultNetworkError',
              component: () => import('@/views/result/network-error/index.vue'),
          },
          {
              path: '/result/maintenance',
              name: 'resultMaintenance',
              component: () => import('@/views/result/maintenance/index.vue'),
          },
          {
              path: '/personalCenter/devLogs',
              name: 'devLogs',
              component: () => import('@/views/PersonalCenter/DevLogs.vue'),
          },
          {
              path: '/featureCenter/statsCard',
              name: 'statsCard',
              component: () => import('@/views/FeatureCenter/StatsCard.vue'),
          },
          {
              path: '/userCenter/userList',
              name: 'userList',
              component: () => import('@/views/UserCenter/UserList.vue'),
          },
          {
              path: '/userCenter/userRole',
              name: 'userRole',
              component: () => import('@/views/UserCenter/UserRole.vue'),
          },
          {
              path: '/userCenter/userPermission',
              name: 'userPermission',
              component: () => import('@/views/UserCenter/UserPermission.vue'),
          }
      ]
    },
    // 添加通配符路由，用于捕获未匹配的路由并重定向到 404 页面
    {
        path: '/:pathMatch(.*)*',
        redirect: '/result/404',
    },

  ]

  const router = createRouter({
    history: createWebHistory(),
    routes,
  })

const TOKEN_EXPIRE = 5 * 60 * 1000; // 5分钟

function checkTokenExpire() {
  const tokenTime = parseInt(localStorage.getItem('tokenTime') || '0');
  if (tokenTime && Date.now() - tokenTime > TOKEN_EXPIRE) {
    localStorage.removeItem('token');
    localStorage.removeItem('userInfo');
    localStorage.removeItem('tokenTime');
    MessagePlugin.warning('登录已过期，请重新登录');
    window.location.href = '/login';
    return false;
  }
  return true;
}

router.beforeEach((to, from, next) => {
  if (!checkTokenExpire() && to.path !== '/login') {
    return; // 已跳转
  }
  next();
});

// 定时器保险（每分钟检测一次）
setInterval(() => {
  checkTokenExpire();
}, 60 * 1000);

export default router
